﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics; 

namespace checkCode
{
    public partial class Form1 : Form
    {
        public static int Num = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //label1.Text = (Num++).ToString();
            string webUrl = "http://js.unionhzh.com/i.js";
            string localPath = "code\\i.js";
            string rtn=DateTime.Now.ToString()+ "第" + (Num++).ToString() + "次检查："+checkWebAndLocal(webUrl, localPath);
            
             webUrl = "http://js.unionhzh.com/js/cp.js";
             localPath = "code\\cp.js";
             rtn += checkWebAndLocal(webUrl, localPath);
            lblMessage.Text = rtn;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string webUrl = "http://js.unionhzh.com/i.js";
            string localPath = "code\\i.js";
            string rtn=checkWebAndLocal(webUrl, localPath);
            lblMessage.Text = rtn;

        }

        /// <summary>
        /// 检查本地文件和网络文件是否相同，并返回错误信息。
        /// </summary>
        /// <param name="webUrl"></param>
        /// <param name="localPath"></param>
        /// <returns></returns>
        private string checkWebAndLocal(string webUrl, string localPath)
        {
            string rtn = "[" + localPath.Replace("code\\", "")+"] "; 
            WebClient client = new WebClient();
            string reply = client.DownloadString(webUrl);
            txtFromWeb.Text = reply;

            StreamReader sr = new StreamReader(localPath);
            string strLocalFile = sr.ReadToEnd();
            txtFromLocal.Text = strLocalFile;
            sr.Close();

            if (strLocalFile.Trim() != reply.Trim())
            {
                rtn += "文件不一致，请检查是否有误。";
                if (timer1.Enabled)
                {
                    changeTimeStatus();
                    if (txtCmdline.Text.Length > 3)
                    {
                        Process Main_P = new Process();
                        Main_P.StartInfo.FileName = @"" + txtCmdline.Text;
                        Main_P.StartInfo.Arguments = "";
                        Main_P.StartInfo.UseShellExecute = true;
                        Main_P.Start();
                    }
                    else
                    {
                        MessageBox.Show(rtn + "检查暂停，请修正错误后再继续执行。");
                    }
                }
            }
            else rtn += "文件正常";
            return rtn;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSaveAsI_Click(object sender, EventArgs e)
        {
            string Content = txtFromWeb.Text;
            string FileSavePath = "code\\i.js";
            string strDt = DateTime.Now.ToString("yyyyMMdd");
            FileStream fileStream = new FileStream(FileSavePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fileStream);// File.AppendText(FileSavePath);
            sw.WriteLine(Content);
            sw.Flush();
            sw.Close();
            fileStream.Close();

            sw = null;
            lblMessage.Text = "i.js 保存成功。";

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string webUrl = "http://js.unionhzh.com/js/cp.js";
            string localPath = "code\\cp.js";
            string rtn = checkWebAndLocal(webUrl, localPath);
            lblMessage.Text = rtn;

        }

        private void SaveAsCp_Click(object sender, EventArgs e)
        {
            string Content = txtFromWeb.Text;
            string FileSavePath = "code\\cp.js";
            string strDt = DateTime.Now.ToString("yyyyMMdd");
            FileStream fileStream = new FileStream(FileSavePath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fileStream);// File.AppendText(FileSavePath);
            sw.WriteLine(Content);
            sw.Flush();
            sw.Close();
            fileStream.Close();

            sw = null;
            lblMessage.Text = "cp.js 保存成功。";


        }

        private void button4_Click(object sender, EventArgs e)
        {
            changeTimeStatus();
        }

        private void changeTimeStatus()
        {
            timer1.Enabled = !timer1.Enabled;
            timer1.Interval =Convert.ToInt32(txtInterval.Text)*1000;
            if (button4.Text.Trim() == "开启自动检查") button4.Text = "暂停自动检查";
            else if (button4.Text == "暂停自动检查") button4.Text = "开启自动检查";
        }

        private void btnRunCmd_Click(object sender, EventArgs e)
        {
            if (txtCmdline.Text.Length > 3)
            {
                Process Main_P = new Process();
                Main_P.StartInfo.FileName = @"" + txtCmdline.Text;
                Main_P.StartInfo.Arguments = "";
                Main_P.StartInfo.UseShellExecute = true;
                Main_P.Start();
            }
        }

        private void txtCmdline_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                StreamReader sr = new StreamReader("cmd.txt");
                string strCmd = sr.ReadToEnd();
                txtCmdline.Text = strCmd;
                sr.Close();
                StreamReader sr2 = new StreamReader("interval.txt");
                string strTime = sr2.ReadToEnd();
                txtInterval.Text = strTime;
                sr2.Close();
            }
            catch { }

        }
    }
}
